﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.XH.SMS;
using YT.Modules;
using YT.DB;
using YT.Commons;
  
namespace YT.XH.NOSE
{
    public partial class frmNSSendMsg : Form
    {
        public frmNSSendMsg()
        {
            InitializeComponent();
        }

        public frmNSSendMsg(string strSickRecID)
        {
            InitializeComponent();
            txtPhoneNo.Text = DataFunctions.GetFieldValue("mSickPerson", "MobilePhone", "SickRecID = '" + strSickRecID + "'");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            YT.XH.SMS.SMS sms = new YT.XH.SMS.SMS();
            string strSend = txtMsg.Text;
            if ( strSend.Length >200) 
            {
                MessageBox.Show ("消息长度不能大于200.","", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
                return ;
            }
            if (strSend == "")
            {
                MessageBox.Show("消息不能为空.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtPhoneNo.Text.Length  == 0 ) return;
            
            DBAccess dbcon = new DBAccess(false, false);


            string[] strPhones = txtPhoneNo.Text.Split( new char[]{','} );
            for (int i = 0; i <= strPhones.Length - 1; i++)
            {
                string strSickRecName = DataFunctions.GetFieldValue(dbcon, "mSickPerson", "SickName", "mobilePhone='" + strPhones[i] + "'");
                sms.SendMsg(strSend, strPhones[i] , strSickRecName);


                string strSickRecID = string.Empty;
                strSickRecID = DataFunctions.GetFieldValue(dbcon, "mSickPerson", "SickRecID", "mobilePhone='" + strPhones[i] + "'");
                if (strSickRecID != "")
                {
                    string strSQL = "select * from mSickPerson where SickRecID='" + strSickRecID + "'";
                    DataRow drSick = dbcon.GetDataFirstRow(strSQL);
                    if (drSick != null)
                    {
                        strSQL = string.Empty;
                        strSQL = @" Insert Into SmsOutbox (SickName,MobilePhone,Message,SendDate,MsgType,SickRecID,SendSuccess) 
                        VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')";
                        strSQL = string.Format(strSQL, drSick["SickName"], strPhones[i], strSend, DateTime.Now, "O", strSickRecID, 1);
                        dbcon.ExecuteSql(strSQL);
                    }
                }
            }
            dbcon.CloseCon();
            MessageBox.Show("消息发送成功。", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            YT.XH.SMS.SMS sms = new YT.XH.SMS.SMS();
            string strReturn = string.Empty ;
            sms.ReadMsg(ref strReturn);
            txtMsg.Text = strReturn;
        }

        private void btnSelMsg_Click(object sender, EventArgs e)
        {
            frmNSMsgMtn frmMsg = new frmNSMsgMtn();
            string strRecID = frmMsg.SelMsg();
            txtMsg.Text = strRecID; 
        }

        private void btnPhoneNo_Click(object sender, EventArgs e)
        {
            frmLookUp frmlookup = new frmLookUp(TitleType.NOSE);
            frmlookup.ShowDialog();
            if (frmlookup.mblnHaveReturnValue)
            {
                txtPhoneNo.Text = frmlookup.GetReturnValue().Trim ();
            }
        }
    }
}
